const cart = {
	state() {
		return {
			goodsNumber: 0,
			cart: [{
				goods_id: "商品id",
				num: "购买数量",
				attr_id: "属性id",
				price: "单价",
				thumn: "商品图片",
				goods_name: "商品名",
				goods_descript: "商品描述",
				// checked: false
			}]
		}
	},

	mutations: {
		addGoods(state, goods) {
			goods.checked = false
			// let cart = state.cart;
			if (state.cart.lengrh == 0) {
				state.cart.push(goods)
			} else {
				let isHas = false;
				for (let i = 0; i < state.cart.length; i++) {
					const cartElem = state.cart[i];
					if (goods.goods_id == cartElem.goods_id && goods.attr_id == cartElem.attr_id) {
						state.cart[i].num++;
						isHas = true;
					}
				}
				if (!isHas) {
					state.cart.push(goods)
				}
			}
		},
		setGoodsChecked(state, index) {
			state.cart[index].checked = !state.cart[index].checked;
		},
		addGoodsNum(state, index) {
			state.cart[index].num++
		},
		subGoodsNum(state, index) {
			if (state.cart[index].num > 1) {
				state.cart[index].num--
			}
		},
		cancelCartChecked(state) {
			for (let i = 0; i < state.cart.length; i++) {
				state.cart[i].checked = false
			}
		},
	},
	actions: {

	},
	getters: {
		cartGoodsNumber(state) {
			let countGoodsNum = 0;
			for (let i = 0; i < state.cart.length; i++) {
				let cartElem = state.cart[i]
				countGoodsNum += cartElem.num
			}
			return countGoodsNum;
		},
		cartGoodsTotal(state) {
			let total = 0;
			for (let i = 0; i < state.cart.length; i++) {
				let cartElem = state.cart[i];
				if (cartElem.checked) {
					total += cartElem.num * cartElem.price
				}
			}
			return total;
		},
		cartAllChecked(state) {
			let all = false;
			let count = 0;
			for (let i = 0; i < state.cart.length; i++) {
				let cartElem = state.cart[i];
				if (cartElem.checked) {
					count++;
				}
			}
			if (state.cart.length == count) {
				all = true
			}
			if (state.cart.length == 0) {
				all = false
			}
			return all
		}
	},
}
export default {
	cart
}
